package predictor;

import java.io.*;
import java.net.*;
import java.util.*;

import org.apache.commons.lang3.StringEscapeUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.*;

import types.Movie;

/**
 * this class get boxoffice information of movies from http://www.proboxoffice.com
 * @author chenji
 *
 */
public class BoxOffice {
	private static String getDir(String link) {
		String dir = "";
		String budget = "";
		String gross = "";
		URL obj;
		try {
			obj = new URL(link);
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();
			con.setConnectTimeout(60000);
			con.setReadTimeout(60000);
			con.setRequestMethod("GET");
			con.setRequestProperty("User-Agent", "Mozilla/5.0");

			BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
			String line;
			while ((line = reader.readLine()) != null) {
				if (line.contains("<h6>Director")) {
					dir = "";
					while ((line = reader.readLine()) != null) {
						dir += line;
						if (line.contains("</ul"))
							break;
					}
					Element director = Jsoup.parse(dir);
					Element ul = director.select("ul").first();
					dir = "";
					for (Element li : ul.select("li")) {
						dir += li.text().trim() + ",";
					}
					dir = dir.substring(0, dir.length() - 1);
				}
				if (line.contains("Total Domestic Gross")) {
					gross = line.substring(line.indexOf("Total Domestic Gross"));
					gross = gross.substring(gross.indexOf("$"));
					gross = gross.substring(0, gross.indexOf("</"));
				}
				if (line.contains("Total Budget")) {
					budget = line.substring(line.indexOf("Total Budget"));
					budget = budget.substring(budget.indexOf("$"));
					budget = budget.substring(0, budget.indexOf("</"));
				}
			}
			reader.close();
			con.disconnect();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			dir = "Dir Not Found!";
			gross = "";
			System.out.println(link);
			e.printStackTrace();
		}
		if (dir.length() == 0)
			dir = "Dir Not Found!";
		return StringEscapeUtils.unescapeHtml4(dir) + "\",\"" + gross + "\",\"" + budget;
	}

	public static void getMovieReleaseTimeFrom(int year) throws Exception {
		File file = new File("movie-release");
		FileOutputStream out = new FileOutputStream(file, true);
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
		Document doc = Jsoup.connect("http://pro.boxoffice.com/statistics/alltime_numbers/domestic/adjusted/" + year)
				.timeout(60 * 1000).get();
		Element table = doc.select("table").first();
		Element tbody = table.select("tbody").first();
		Element movie;
		String title, release;
		String[] rs;
		for (Element row : tbody.select("tr")) {
			// System.out.println(row.toString());
			movie = row.select("td").get(1).select("a").first();
			title = movie.text();
			release = row.select("td").get(5).text();
			rs = release.split(" ");
			release = rs[1].substring(0, rs[1].length() - 1) + " " + rs[0] + " " + rs[2];
			title = StringEscapeUtils.unescapeHtml4(title.trim());
			while (title.matches(".*\\([^\\)]*\\)$")) {
				title = title.substring(0, title.lastIndexOf("(")).trim();
			}
			System.out.println("\"" + title + "\",\"" + release + "\"\n");
			writer.write("\"" + title + "\",\"" + release + "\"\n");
		}
		writer.flush();
		writer.close();
	}

	public static void getMovieGrossFrom(int year) throws Exception {
		File file = new File("movie-gross");
		FileOutputStream out = new FileOutputStream(file, true);
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
		Document doc = Jsoup.connect("http://pro.boxoffice.com/statistics/alltime_numbers/domestic/adjusted/" + year)
				.timeout(60 * 1000).get();
		Element table = doc.select("table").first();
		Element tbody = table.select("tbody").first();
		Element movie;
		String title, link, gross, studio, dir;
		for (Element row : tbody.select("tr")) {
			// System.out.println(row.toString());
			movie = row.select("td").get(1).select("a").first();
			link = movie.attr("abs:href");
			title = movie.text();
			gross = row.select("td.number").first().text();
			dir = getDir(link);
			title = StringEscapeUtils.unescapeHtml4(title.trim());
			studio = title.substring(title.lastIndexOf("(") + 1, title.lastIndexOf(")"));
			while (title.matches(".*\\([^\\)]*\\)$")) {
				title = title.substring(0, title.lastIndexOf("(")).trim();
			}
			// System.out.println("\"" + title + "\",\"" + studio + "\",\"" + year +
			// "\",\"" + dir + "\",\"" + gross + "\"");
			writer.write("\"" + title + "\",\"" + studio + "\",\"" + year + "\",\"" + dir + "\",\"" + gross + "\"\n");
		}
		writer.flush();
		writer.close();
	}

	public static List<Movie> readMovies() throws Exception {
		File file = new File("movie-gross");
		FileInputStream in = new FileInputStream(file);
		BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
		String line = "";
		List<Movie> list = new LinkedList<Movie>();
		String title, dir, year, studio, gross, budget, gross_adj;
		while ((line = reader.readLine()) != null) {
			String[] ls = line.split("\",\"");
			title = ls[0].substring(1);
			studio = ls[1];
			year = ls[2];
			dir = ls[3];
			gross = ls[4];
			budget = ls[5];
			gross_adj = ls[6].substring(0, ls[6].length() - 1);
			//System.out.println(title + "," + dir + "," + gross + "," + budget);
			list.add(new Movie(title, dir, Integer.parseInt(year)).studio(studio).gross(gross).gross_adj(gross_adj)
					.budget(budget));
		}
		reader.close();
		
		file = new File("movie-release");
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
		String release;
		int i = 0;
		while ((line = reader.readLine()) != null) {
			String[] ls = line.split("\",\"");
			title = ls[0].substring(1);
			release = ls[1].substring(0, ls[1].length() - 1);
			list.get(i).release(release).aka(title);
//			if (!list.get(i).getTitle().startsWith("#") && !list.get(i).getTitle().equals(title)) {
//				System.out.println(list.get(i).getTitle() + " -- " + title);
//			}
			i ++;
		}
		reader.close();
		
//		for (Movie m : list) {
//			System.out.println(m.getTitle() + "," + m.getDir() + "," + m.getGross() + "," + m.getRelease());
//		}
		return list;
	}

	public static void main(String[] args) {
		try {
			for (int i = 2000; i <= 2014; i++) {
				// getMovieGrossFrom(i);
				getMovieReleaseTimeFrom(i);
				System.out.println(i + ": Finished");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
